<template>
  <!-- 案例类型 -->
  <basic-container>
    <el-row :gutter="20">
      <el-col :span="6">
        <el-card shadow="always" class="top-wrap bg1">
          <el-row :gutter="20">
            <el-col :span="12">
              <div class="left-style">待诊断</div>
              <div class="left-name">{{ count2 }} </div>
            </el-col>
            <el-col :span="12">
              <div class="right-style">
                <el-image :src="url"></el-image>
              </div>
            </el-col>
          </el-row>
        </el-card>

      </el-col>
      <el-col :span="6">
        <el-card shadow="always" class="top-wrap bg2">
          <el-row :gutter="20">
            <el-col :span="12">
              <div class="left-style">待检查</div>
              <div class="left-name">{{ count1 }} </div>
            </el-col>
            <el-col :span="12">
              <div class="right-style">
                <el-image :src="url"></el-image>
              </div>
            </el-col>
          </el-row></el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="always" class="top-wrap bg3">
          <el-row :gutter="20">
            <el-col :span="12">
              <div class="left-style">待复查</div>
              <div class="left-name">{{ count3 }} </div>
            </el-col>
            <el-col :span="12">
              <div class="right-style">
                <el-image :src="url"></el-image>
              </div>
            </el-col>
          </el-row></el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover" class="top-wrap bg4">
          <el-row :gutter="20">
            <el-col :span="12">
              <div class="left-style">创建档案</div>
            </el-col>
            <el-col :span="12">
              <div class="right-add">
                <el-image @click="addDangan()" :src="addurl"></el-image>
              </div>
            </el-col>
          </el-row></el-card>
      </el-col>
    </el-row>
    <avue-crud :option="option" :table-loading="loading" :data="data" :page.sync="page" :permission="permissionList"
      :before-open="beforeOpen" v-model="form" ref="crud" @row-update="rowUpdate" @row-save="rowSave" @row-del="rowDel"
      @search-change="searchChange" @search-reset="searchReset" @selection-change="selectionChange"
      @current-change="currentChange" @size-change="sizeChange" @refresh-change="refreshChange" @on-load="onLoad">
      <template slot-scope="scope" slot="menu">
        <el-button type="text" size="small" icon="el-icon-view"
          @click.native="handleDetail(scope.row, scope.index)">详情</el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import { getCountByStatus, getList, getDetail, add, update, remove } from "@/api/infoManagement/patientFile";
import { mapGetters } from "vuex";
import website from "@/config/website";
import { getAge } from '@/util/date.js'

export default {
  data() {
    return {
      count1: 0,
      count2: 0,
      count3: 0,
      count4: 0,
      count5: 0,
      url: '/img/indexicon.png',
      addurl: '/img/add2.png',
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      viewbtn: false,
      selectionList: [],
      option: {
        height: 'auto',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchSpan: 5,
        searchMenuSpan: 5,
        border: true,
        index: true,
        viewBtn: false,
        addBtn: false,
        editBtn: false,
        delBtn: false,
        selection: false,
        refreshBtn: false,
        columnBtn: false,
        searchShowBtn: false,
        dialogClickModal: false,
        column: [
          {
            label: "患者姓名",
            prop: "name",
          },
          {
            label: "性别",
            prop: "sex",
            type: "select",
            props: {
              label: 'name',
              value: 'code'
            },
            dicData: [{
              name: '男',
              code: 1
            }, {
              name: '女',
              code: 0
            }],
            rules: [{
              required: true,
              message: "请选择",
              trigger: "blur"
            }]
          },
          {
            label: "出生日期",
            prop: "birthday",
            type: 'date',
            hide: true,
            valueFormat: 'yyyy-MM-dd',
          },
          {
            label: "民族",
            prop: "nation",
            hide: true,
          },
          {
            label: "身份证号",
            prop: "nation",
            hide: true,
          },
          {
            label: "身高",
            prop: "nation",
            hide: true,
          },
          {
            label: "体重",
            prop: "nation",
            hide: true,
          }, {
            label: "年龄",
            prop: "birthday",
            search: true,
            formatter(val, value) {
              return getAge(value)
            }
          },
          {
            label: "联系人",
            prop: "nation",
            hide: true,
          },
          {
            label: "联系电话",
            prop: "nation",
            hide: true,
          },



          {
            label: '建档时间',
            prop: 'createTime',
            type: 'datetime',
            addDisplay: false,
            valueFormat: 'yyyy-MM-dd HH:mm:ss',
            rules: [
              {
                required: true,
                message: '请选择',
                trigger: 'blur'
              }
            ]

          },
          {
            label: "常住地址",
            prop: "address",
            type: "textarea",
            span: 24,
            minRows: 6,

          },
          {
            label: "状态",
            prop: "status",
            type: "select",
            props: {
              label: 'name',
              value: 'code'
            },
            dicData: [{
              name: '待检查',
              code: 1
            }, {
              name: '待诊断',
              code: 2
            }, {
              name: '待复查',
              code: 3
            }, {
              name: '康复中',
              code: 4
            }, {
              name: '已康复',
              code: 5
            }],
            rules: [{
              required: true,
              message: "请选择",
              trigger: "blur"
            }]
          },
        ]
      },
      data: []
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      // this.viewbtn = true
      // this.viewbtn = this.vaildData(this.permission.device_view, false)
      return {
        viewbtn: true
        // addBtn: this.vaildData(this.permission.post_add, false),
        // viewBtn: this.vaildData(this.permission.post_view, false),
        // delBtn: this.vaildData(this.permission.post_delete, false),
        // editBtn: this.vaildData(this.permission.post_edit, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  mounted() {
    getCountByStatus(1).then((res) => {
      const data = res.data.data
      this.count1 = data
    })
    getCountByStatus(2).then((res) => {
      const data = res.data.data
      this.count2 = data
    })
    getCountByStatus(3).then((res) => {
      const data = res.data.data
      this.count3 = data
    })
    getCountByStatus(4).then((res) => {
      const data = res.data.data
      this.count4 = data
    })
    getCountByStatus(5).then((res) => {
      const data = res.data.data
      this.count5 = data
    })

  },
  methods: {
    handleDetail(row) {
      this.$router.push({
        path: '/patientFile/detail',
        query: {
          id: row.id,
          title: '档案详情'
        },
      })
    },
    addDangan() {
      this.$router.push({
        path: '/patientFile/add',
      })
    },
    rowSave(row, done, loading) {
      add(row).then(() => {
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
        done();
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(() => {
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
        done();
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getDetail(this.form.id).then(res => {
          this.form = res.data.data;
        });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    }
  }
};
</script>

<style scoped>
.top-wrap {
  position: relative;
  width: 100%;
  height: 125px;
  color: #fffFFF;
}

.bg1 {
  background-color: #4acf5a;
}

.bg2 {
  background-color: #ffbd44;
}

.bg3 {
  background-color: #4a91cf;
}

.bg4 {
  background-color: #8f4acf;

}

.left-style {
  font-size: 22px;
}

.left-name {
  font-size: 28px;
  text-align: center;
  padding: 10px;
}

.right-style {
  width: 70px;
  height: 70px;
  padding: 10px 30px;
}

.right-add {
  width: 80px;
  height: 80px;
  cursor: pointer;
}
</style>
